package model

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"homeword1103/config"
)

var (
	DB  *gorm.DB
	err error
)

func Init() {
	mysqlConfig := config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlConfig.User,
		mysqlConfig.Password,
		mysqlConfig.Host,
		mysqlConfig.Port,
		mysqlConfig.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	fmt.Println("数据库连接成功")
	if err = DB.AutoMigrate(
		&User{},
	); err != nil {
		panic(err)
	}
	fmt.Println("数据表迁移成功")
}
